package com.showsu.editor.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.showsu.editor.dao.TblXhsAwemeDetailRecordMapper;
import com.showsu.editor.entity.po.TblXhsAwemeDetailRecord;
import com.showsu.editor.entity.vo.request.RequestVO;
import com.showsu.editor.entity.vo.response.XhsLiveVO;
import com.showsu.editor.enums.RowFlagEnum;
import com.showsu.editor.service.IXhsAwemeDetailRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author: jph
 * @Description: xhs笔记详情查询记录表
 * @Date: Created in 16:04 下午 2025/08/14
 * @Modified By:
 */
@Service
@Slf4j
public class XhsAwemeDetailRecordServiceImpl extends ServiceImpl<TblXhsAwemeDetailRecordMapper, TblXhsAwemeDetailRecord> implements IXhsAwemeDetailRecordService {
    

    @Autowired
    private TblXhsAwemeDetailRecordMapper mapper;

    @Override
    public List<TblXhsAwemeDetailRecord> queryList(Long userId) {
        return this.queryList(userId,null);
    }


    @Override
    public List<TblXhsAwemeDetailRecord> queryList(Long userId,Integer limit) {
        if (userId == null) {
            return null;
        }
        LambdaQueryWrapper<TblXhsAwemeDetailRecord> wrapper=new QueryWrapper<TblXhsAwemeDetailRecord>().lambda()
                .eq(TblXhsAwemeDetailRecord::getUserId, userId)
                .eq(TblXhsAwemeDetailRecord::getFlag, RowFlagEnum.UnDelete.getCode())
                .orderByDesc(TblXhsAwemeDetailRecord::getId);
        if(limit!=null){
            wrapper.last("LIMIT "+limit);
        }
        return mapper.selectList(wrapper);
    }


    @Override
    public Integer queryCount(Long userId, String startTime, String endTime) {
        if (userId == null || startTime == null || endTime == null) {
            return 0;
        }
        return mapper.selectCount(new QueryWrapper<TblXhsAwemeDetailRecord>().lambda()
                .eq(TblXhsAwemeDetailRecord::getUserId, userId)
                .ge(TblXhsAwemeDetailRecord::getCreateTime, startTime)
                .le(TblXhsAwemeDetailRecord::getCreateTime, endTime)
                .eq(TblXhsAwemeDetailRecord::getFlag, RowFlagEnum.UnDelete.getCode()));
    }

    @Override
    public Integer queryCount(Long userId, Date startDate, Date endDate) {
        if (userId == null) {
            return 0;
        }
        return mapper.selectCount(new QueryWrapper<TblXhsAwemeDetailRecord>().lambda()
                .eq(TblXhsAwemeDetailRecord::getUserId, userId)
                .eq(TblXhsAwemeDetailRecord::getFlag, RowFlagEnum.UnDelete.getCode())
                .between(TblXhsAwemeDetailRecord::getCreateTime, startDate, endDate));
    }

    @Override
    public Integer queryCount(Long userId) {
        if (userId == null) {
            return 0;
        }
        return mapper.selectCount(new QueryWrapper<TblXhsAwemeDetailRecord>().lambda()
                .eq(TblXhsAwemeDetailRecord::getUserId, userId)
                .eq(TblXhsAwemeDetailRecord::getFlag, RowFlagEnum.UnDelete.getCode()));
    }

    @Override
    public Long saveRecord(RequestVO clientRequest, Long userId, TblXhsAwemeDetailRecord result, Long cost) {
        TblXhsAwemeDetailRecord record = result;
        if(record==null){
            record=new TblXhsAwemeDetailRecord();
        }
        record.setId(null);
        record.setClientBrowser(clientRequest.getClientBrowser());
        record.setClientIp(clientRequest.getClientIp());
        record.setClientOrigin(clientRequest.getClientOrigin());
        record.setClientOs(clientRequest.getClientOs());
        record.setCost(cost);
        record.setUserId(userId);
        mapper.insert(record);
        return record.getId();
    }
}
